home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AdobeExamples / NX_Patterns / PSWpatternsdemo.psw < prev    next >
Encoding:
Text File  |  1992-12-19  |  9.1 KB  |  331 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    PSWpatternsdemo.psw
  35.  *
  36.  *    These PostScript procedures are for the demonstration program only.
  37.  *    They are not the recommended approach for displaying patterns. The
  38.  *    font approach outlined in PSWpatterns.psw should be used instead.
  39.  *
  40.  *    The composite method is not recommended because it is not much
  41.  *    faster than the font approach, it is difficult to work with and it will not
  42.  *    print. The drawing approach is not recommended because it is slow.
  43.  *
  44.  *    Version:    2.0
  45.  *    Author:    Ken Fromm
  46.  *    History:
  47.  *            03-07-91        Added this comment.
  48.  */
  49.  
  50.  defineps PSWPatternDemoDefs ()
  51.  
  52.    %
  53.     %    This dictionary is for using the composite method of pattern display.
  54.     %  It is fast provided the cell is not skewed. Only one pass is needed
  55.     %  even for colored patterns. Problems arise though in trying to tile
  56.     %    up the patterns. Plus this approach won't print.
  57.     %
  58.     /PatternImageDict
  59.         8 dict begin
  60.  
  61.     /PATImageDict 1 dict def
  62.  
  63.     % PATsi - sets the image in the dictionary
  64.     /PATsi {         % gstate PATsi -
  65.         PATImageDict /CurrentImage 3 -1 roll put
  66.     } bind def
  67.  
  68.     /PATdrawcell {
  69.         newpath
  70.         BBox aload pop
  71.         2 index sub exch
  72.         3 index sub exch
  73.         PATrectdraw
  74.     } bind def
  75.  
  76.     /PATsize {     % patdict  PATsize  x y w h
  77.         begin
  78.             gsave
  79.                 matrix currentmatrix    % matrix
  80.                 PatternGState PATsg
  81.                 PATdrawcell
  82.                  setmatrix                % -
  83.  
  84.                 pathbbox            % llx lly urx ury
  85.                 2 index sub exch    % llx lly h urx
  86.                 3 index sub exch    % llx lly w h
  87.             grestore
  88.         end
  89.     } bind def    
  90.  
  91.     % PATpicalc - calculates the starting point and width/height of the tile fill for the shape
  92.     /PATpicalc { % - PATpicalc nw nh px py
  93.         PATDict /CurrentPattern get begin
  94.             pathbbox        % llx lly urx ury
  95.  
  96.             % Determine (nw, nh) the number of cells to paint width and height
  97.             PatHeight div ceiling 1 add    % llx lly urx qh 
  98.             4 1 roll                % qh llx lly urx
  99.             PatWidth div ceiling 1 add    % qh llx lly qw
  100.             4 1 roll                % qw qh llx lly
  101.             PatHeight div floor        % qw qh llx ph
  102.             4 1 roll                % ph qw qh llx
  103.             PatWidth div floor        % ph qw qh pw
  104.             4 1 roll                % pw ph qw qh
  105.             2 index sub cvi abs     % pw ph qw qh-ph
  106.             exch 3 index sub cvi abs exch    % pw ph nw=qw-pw nh=qh-ph 
  107.  
  108.             % Determine the starting point of the pattern fill (px, py)
  109.             4 2 roll                % nw nh pw ph
  110.             PatHeight mul PatHeight sub    % nw nh pw py
  111.             exch                % nw nh py pw
  112.             PatWidth mul PatWidth sub    % nw nh py px
  113.             exch                 % nw nh px py
  114.         end
  115.     } bind def
  116.  
  117.     % PATtile - performs the tiling for the shape
  118.     /PATtile { % nw nh px py PATtile -
  119.         PATDict /CurrentPattern get dup begin
  120.             setfont
  121.  
  122.             matrix currentmatrix
  123.             PatternGState PATsg        % but keep the same matrix
  124.             setmatrix
  125.  
  126.             PaintType 2 eq { PATDict /PColor get PATsc } if
  127.  
  128.             0 1 NumSources 1 sub {    % nw nh px py source
  129.                 2 index 2 index         % nw nh px py source px py
  130.                 moveto                % nw nh px py source
  131.  
  132.                 Multi { dup PaintColors exch get PATsc } if
  133.  
  134.                 0 1 PATfstr length 1 sub { 1 index PATfstr 3 1 roll put } for pop
  135.                 2 index                % nw nh px py nh
  136.                 {                    % nw nh px py
  137.                     currentpoint
  138.                     5 index            % nw nh px py cx cy nw
  139.                     {                % nw nh px py
  140.                         PATfstr oldshow
  141.                     } repeat
  142.                     YStep add moveto
  143.                 } repeat
  144.             } for
  145.             pop pop pop pop
  146.         end
  147.     } bind def
  148.  
  149.     /PATimage{     % patdict PATimage gstate
  150.         begin
  151.             gsave                
  152.                 currentdict PATsize    % x y w h
  153.                 4 2 roll pop pop        % w h
  154.                 transform                % w' h'
  155.                 PatternGState PATsg    
  156.                 itransform translate        % -
  157.  
  158.                 gsave
  159.                     PATdrawcell        
  160.                     PATpicalc        % nw nh px py
  161.                     clip
  162.                     newpath
  163.                     PATtile            % -
  164.                 grestore
  165.                 gstate currentgstate    % gstate
  166.             grestore
  167.         end
  168.     } bind def    
  169.  
  170.     % PATfill - performs the tiling for the shape
  171.     /PATfill { % nw nh px py PATfill -
  172.         PATDict /CurrentPattern get begin
  173.  
  174.             % Set up the coordinate system to Pattern Space
  175.             PatternGState PATsg
  176.  
  177.             % Translate so that the compositing places the cell correctly
  178.             BBox 0 get BBox 1 get translate
  179.  
  180.             % Move to the starting location
  181.             moveto            % nw nh
  182.  
  183.             BBox aload pop        % nw nh llx lly urx ury
  184.             2 index sub exch    % nw nh llx lly h urx
  185.             3 index sub exch    % nw nh llx lly w h
  186.             4 index            % nw nh llx lly w h nh
  187.             {                % nw nh llx lly w h
  188.                 currentpoint    % nw nh llx lly w h cx cy
  189.                 6 2 roll        % nw nh cx cy llx lly w h
  190.                 7 index        % nw nh cx cy llx lly w h nw
  191.                 {            % nw nh cx cy llx lly w h
  192.                     4 copy 
  193.                     PATImageDict /CurrentImage get
  194.                     currentpoint 1 composite    % -
  195.                     XStep 0 rmoveto
  196.                 } repeat
  197.                 6 -2 roll moveto
  198.                 0 YStep rmoveto
  199.             } repeat
  200.             pop pop pop pop pop pop
  201.         end
  202.     } bind def
  203.  
  204.     currentdict
  205.     end
  206.     def
  207.  
  208.     %
  209.     %    Do not use this method. It is much slower than the font and composite
  210.     %    approaches. The font method takes advantage of the font cache. This
  211.     %    method draws each cell each time. It takes a long time to display which
  212.     %    means it would take FOREVER to print a pattern this way.
  213.     %
  214.     /PatternDrawDict
  215.     1 dict begin
  216.  
  217.     % PATfill - performs the tiling for the shape
  218.     /PATfill { % nw nh px py PATfill -
  219.         false PATredef
  220.         PATDict /CurrentPattern get begin
  221.  
  222.             % Set up the coordinate system to Pattern Space
  223.             PatternGState PATsg
  224.  
  225.             % Set the color for uncolored patterns
  226.             PaintType 2 eq { PATDict /PColor get PATsc } if
  227.  
  228.             % Loop for each of the pattern sources
  229.             0 1 NumSources 1 sub {    % nw nh px py source
  230.                 % Move to the starting location
  231.                 2 index 2 index         % nw nh px py source px py
  232.                 moveto                % nw nh px py source
  233.  
  234.                 % For multiple sources, set the appropriate color
  235.                 Multi { dup PaintColors exch get PATsc } if
  236.  
  237.                 Multi { PaintData exch get } if    % nw nh px py [paintdata]
  238.                 currentdict            % nw nh px py [paintdata] dict
  239.  
  240.                 % Loop over the number of vertical cells
  241.                 4 index                % nw nh px py [paintdata] dict nh
  242.                 {                    % nw nh px py [paintdata] dict
  243.                     % Loop over the number of horizontal cells
  244.                     currentpoint
  245.                     7 index            % nw nh px py [paintdata] dict cx cy nw
  246.                     {                % nw nh px py [paintdata] dict cx cy
  247.                         gsave
  248.                         currentpoint translate
  249.                         mark        % nw nh px py [paintdata] dict cx cy mark
  250.                         4 index        % nw nh px py [paintdata] dict cx cy mark [paintdata]
  251.                         4 index        % nw nh px py [paintdata] dict cx cy mark [paintdata] dict
  252.                         PaintProc    % nw nh px py [paintdata] dict cx cy mark 
  253.                         cleartomark    % nw nh px py [paintdata] dict cx cy
  254.                         grestore
  255.                         XStep 0 rmoveto
  256.                     } repeat
  257.                     YStep add moveto
  258.                 } repeat pop pop
  259.             } for
  260.             pop pop pop pop
  261.         end
  262.          true PATredef
  263.      } bind def
  264.  
  265.     currentdict
  266.     end
  267.     def
  268.  
  269. endps
  270.  
  271. /*****************************************************
  272. *
  273. *    The wraps called from the pattern object.
  274. *
  275. *****************************************************/
  276.  
  277. defineps PSWBeginPatternImage()
  278.     PatternImageDict begin
  279. endps
  280.  
  281. defineps PSWSetPatternImage(userobject PatternImageNum)
  282.     PatternImageNum  PATsi
  283. endps
  284.  
  285. /*
  286. *    Sizes the image for the pattern in the current window. Assumes the
  287. *    PatternDict is already on the dictionary stack.
  288. */
  289. defineps PSWSizeImage(userobject PatternNum | float *X, *Y, *W, *H)
  290.     PatternNum  PATsp
  291.     PatternImageDict begin
  292.         PatternNum PATsize H W Y X
  293.     end
  294.     PATusp
  295. endps
  296.  
  297. /*
  298. *    Make an image of the pattern in the current window. The image
  299. *    will be composited into the clipping path to produce the pattern.
  300. *    Leaves a gstate object on the stack. Assumes the PatternDict is
  301. *    already on the dictionary stack.
  302. */
  303. defineps PSWMakeImage(userobject PatternNum)
  304.     PatternNum  PATsp
  305.     PatternImageDict begin
  306.         PatternNum PATimage
  307.     end
  308.     PATusp
  309. endps
  310.  
  311.  
  312. defineps PSWBeginPatternDraw()
  313.     PatternDrawDict begin
  314. endps
  315.  
  316.  
  317. defineps PSWCompositePattern(userobject PatternNum, PatternImageNum; float X, Y)
  318.     PatternNum  begin
  319.         BBox aload pop                % llx lly urx ury
  320.         2 index sub exch            % lx lly h urx
  321.         3 index sub exch            % llx lly w h
  322.         2 copy PatternGState /cm get % llx lly w h matrix
  323.         dtransform                    % llx lly w h w' h'
  324.         idtransform                % llx lly w h w* h*
  325.         Y exch 2 div sub exch        % llx lly w h y* w* 
  326.         X exch 2 div sub exch        % llx lly w h x* y*
  327.         PatternImageNum 3 1 roll    % llx lly w h gstate x* y*
  328.         1 composite                % -
  329.     end
  330. endps
  331.